package com.baoxin.chenxixc4coach;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.baoxin.chenxixc4coach.java.MyHandler;
import com.baoxin.chenxixc4coach.utils.Interfaces;
import com.baoxin.chenxixc4coach.utils.Msg;
import com.baoxin.chenxixc4coach.utils.NetUtil;
import com.baoxin.chenxixc4coach.views.CustomDatePicker;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import okhttp3.Callback;

public class AgendaEditActivity extends BaseActivity implements View.OnClickListener {
    private RelativeLayout selectDate, selectTime;
    private TextView currentDate, currentTime;//日程日期，提醒时间，用当前时间初始化
    private CustomDatePicker datePicker, timePicker;
    private String time;
    private String date;
    private TextView mTitle;
    private TextView mMark;
    private Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_agenda_edit);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        selectTime = (RelativeLayout) findViewById(R.id.selectTime);
        selectTime.setOnClickListener(this);
        selectDate = (RelativeLayout) findViewById(R.id.selectDate);
        selectDate.setOnClickListener(this);
        currentDate = (TextView) findViewById(R.id.currentDate);
        currentTime = (TextView) findViewById(R.id.currentTime);
        mTitle = findViewById(R.id.title);
        mMark = findViewById(R.id.mark);

        handler = new MyHandler(this, activity) {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case Msg.REQUEST_SUCCEED:
                        Toast.makeText(activity, "添加日程成功！", Toast.LENGTH_SHORT).show();
                        setResult(0);
                        finish();
                        break;
                }
            }
        };
        initPicker();
    }

    private void initPicker() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm", Locale.CHINA);
        time = sdf.format(new Date());
        date = time.split(" ")[0];
        //设置当前显示的日期
        currentDate.setText(date);
        //设置当前显示的时间
        currentTime.setText(time);

        /**
         * 设置年月日
         */
        datePicker = new CustomDatePicker(this, "请选择日期", new CustomDatePicker.ResultHandler() {
            @Override
            public void handle(String time) {
                currentDate.setText(time.split(" ")[0]);
            }
        }, "2007/01/01 00:00", time);
        datePicker.showSpecificTime(false); //显示时和分
        datePicker.setIsLoop(false);
        datePicker.setDayIsLoop(true);
        datePicker.setMonIsLoop(true);

        timePicker = new CustomDatePicker(this, "请选择时间", new CustomDatePicker.ResultHandler() {
            @Override
            public void handle(String time) {
                currentTime.setText(time);
            }
        }, "2007/01/01 00:00", "2027/12/31 23:59");//"2027-12-31 23:59"
        timePicker.showSpecificTime(true);
        timePicker.setIsLoop(true);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.selectDate:
                // 日期格式为yyyy/MM/dd
                datePicker.show(date);
                break;

            case R.id.selectTime:
                // 日期格式为yyyy/MM/dd HH:mm
                timePicker.show(time);
                break;
        }
    }

    public void onSave(View view) {
        if (mTitle.getText().length() == 0) {
            Toast.makeText(activity, "标题不能为空！", Toast.LENGTH_SHORT).show();
            return;
        }
        JSONObject body = new JSONObject();
        try {
            body.put("rsqTitle", mTitle.getText().toString());
            body.put("rsqContext", mMark.getText().toString());
            body.put("rsqTime", currentDate.getText().toString());
            body.put("rsqRemindTime", currentTime.getText().toString());
            NetUtil.post(activity, Interfaces.URL_ADD_AGENDA, body.toString(), new MyCallback(Interfaces.URL_ADD_AGENDA, handler));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
